﻿// -----------------------------------------------------------------------
// <copyright file="Mine.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SpaceshipDuel
{
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Mine : GameObject
    {
        #region Private fields

        int radius;             // Radius (für erste Stufe der Kollisionserkennung)
        int structIntegrity;    // "Lebenspunkte" der Mine, bei 0 wird sie zerstört
        float rotationSpeed;    // Rotationsgeschwindigkeit
        float rotationAngle;    // Rotationswinkel 
        bool destroyMe;         // Flag zum Löschen
        Vector2 velocity;       // Geschwindigkeitsvektor
        WaitTimer animTimer;
        int frameCount;
        bool startDetonation;

        #endregion Private fields

        #region Public properties

        public bool DestroyMe
        {
            get { return this.destroyMe; }
            set { this.destroyMe = value; }
        }

        public int Radius
        {
            get { return this.radius; }
            set { this.radius = value; }
        }

        public float RotationAngle
        {
            get { return this.rotationAngle; }
            set { this.rotationAngle = value; }
        }

        public bool StartDetonation
        {
            get { return this.startDetonation; }
            set { this.startDetonation = value; }
        }

        #endregion Public properties

        #region Construction


        public Mine(TextureData newTextureData, Vector2 newCenter, int newWorldSize, Random rnd)
        {
            destroyMe = false;
            textureData = newTextureData;
            radius = textureData.width / textureData.tileCountX / 2;
            center = newCenter;
            worldSize = newWorldSize;
            rotationAngle = rnd.Next(180);
            rotationSpeed = (rnd.Next(40) + 10) / 1000f;
            velocity = new Vector2((rnd.Next(15)+5) / 40f, (rnd.Next(15)+5) / 40f);
            structIntegrity = 200;
            animTimer = new WaitTimer(0.01f);
            frameCount = 0;
            startDetonation = false;
        }

        #endregion Construction

        #region Public methods

        /// <summary>
        /// Update-Methode
        /// </summary>
        public void Update(GameTime gameTime)
        {
            if (destroyMe == false)
            {
                center += velocity;
                CheckPosition();

                rotationAngle += rotationSpeed;
                rotationAngle = (float)(rotationAngle % (Math.PI * 2));

                if (structIntegrity <= 0 && startDetonation == false)
                {
                    //animTimer.Reset();
                    startDetonation = true;
                }

                if (startDetonation == true)
                {
                    if (frameCount >= textureData.tileCountX * textureData.tileCountY)
                        destroyMe = true;
                    else if (animTimer.IsElapsed(gameTime.ElapsedGameTime.TotalSeconds) == true)
                        frameCount++;
                }
            }
        }

        /// <summary>
        /// Draw-Methode
        /// </summary>
        /// <param name="spriteBatch">Spritebacth</param>
        /// <param name="xOffset">Offset X</param>
        /// <param name="yOffset">Offset Y</param>
        new public void Draw(SpriteBatch spriteBatch, float xOffset, float yOffset)
        {
            if (destroyMe == false)
            {
                Vector2 position = new Vector2(center.X + xOffset, center.Y + yOffset);
                Vector2 origin = new Vector2(textureData.tileWidth / 2, textureData.tileHeight / 2);
                Rectangle sourceRect = new Rectangle(frameCount % textureData.tileCountX * textureData.tileWidth, frameCount / textureData.tileCountX * textureData.tileHeight, textureData.tileWidth, textureData.tileHeight);
                spriteBatch.Draw(textureData.texture, position, sourceRect, Color.White, rotationAngle, origin, 1.0f, SpriteEffects.None, 0f);
            }
        }

        // Treffer
        public void Hit(int damage = 1)
        {
            structIntegrity -= damage;
        }

        #endregion Public methods

        #region Private methods

        /// <summary>
        /// Korrektur der Position, wenn das Objekt über den Spielfeldrand hinausfliegt
        /// </summary>
        private void CheckPosition()
        {
            if (center.X < 0)
            {
                center.X = worldSize + center.X;
            }
            else if (center.X >= worldSize)
            {
                center.X = center.X - worldSize;
            }

            if (center.Y < 0)
            {
                center.Y = worldSize + center.Y;
            }
            else if (center.Y >= worldSize)
            {
                center.Y = center.Y - worldSize;
            }
        }

        #endregion Private methods
    }
}
